import gbk from "./gbk.js";
import CryptoJS from "crypto-js";

function isAscii(oneByte) {
	return oneByte <= 127 && oneByte >= 0;
}

function trimRight(str) {
	return str.replace(/\s+$/, "");
}

function trimLeft(str) {
	return str.replace(/^\s+/, "");
}

function trim(str) {
	return trimRight(trimLeft(str));
}

function desensitizeSubstring(str, start, end) {
	if (end == -1) {
		end = str.length;
	}

	let len = end - start;
	if (len > 5) len = 5;
	let desenStr = str.substr(0, start) + "*".repeat(len) + str.substr(end);
	return desenStr;
}

function readTagAsString(byteArray, position, length, vr = "") {
	let result = "";
	let byte;
	let hexStr;

	if (vr === "SS") {
		const vm = length / 2;

		for (let i = 0; i < vm; i++) {
			const lowByte = byteArray[position + i * 2];
			const highByte = byteArray[position + i * 2 + 1];
			const val = (highByte << 8) | lowByte;
			const str = val.toString(16);
			result += str;
			if (i < vm - 1) {
				result += "\\";
			}
		}

		return result;
	}

	for (let i = 0; i < length; i++) {
		byte = byteArray[position + i];
		hexStr = byteArray[position + i].toString(16);
		if (byte === 0) {
			position += length;
			return result;
		}
		if (isAscii(byte)) {
			result += String.fromCharCode(byte);
		} else {
			if (hexStr.length === 1) hexStr = "0" + hexStr;
			result += "%" + hexStr.toUpperCase();
		}
	}
	return result;
}

function decodeChinese(str, isUtf8, isUri = false) {
	if (isUtf8) {
		if (isUri) {
			return decodeURI(str);
		} else {
			return decodeURIComponent(str);
		}
	} else {
		return gbk.decode(str);
	}
}

function formartDcmDate(dateStr) {
	if (!dateStr) return "";
	return (
		dateStr.substring(0, 4) +
		"-" +
		dateStr.substring(4, 6) +
		"-" +
		dateStr.substring(6)
	);
}

function formartDcmTime(timeStr) {
	if (!timeStr) return "";
	return (
		timeStr.substring(0, 2) +
		":" +
		timeStr.substring(2, 4) +
		":" +
		timeStr.substring(4)
	);
}

function IsMobile() {
	return /Android|webOS|iPhone|iPad|iPod|Windows Phone|BlackBerry|SymbianOS/i.test(
		navigator.userAgent
	);
}

function IsPhone() {
	const w = window.screen.width;
	return IsMobile() && w < 768;
}

function IsPC() {
	return !IsMobile();
}

function getNSpace(n) {
	let sp = "";
	for (let i = 0; i < n; i++) {
		sp += "\xa0\xa0";
	}
	return sp;
}

function formatDate(date, format = "YYYY-MM-DD hh:mm:ss") {
	if (typeof date === "string") {
		date = new Date(date);
	}

	const d = date;
	let str = format;

	const Week = ["日", "一", "二", "三", "四", "五", "六"];

	str = str.replace(/yyyy|YYYY/, d.getFullYear());
	str = str.replace(
		/yy|YY/,
		d.getYear() % 100 > 9 ? (d.getYear() % 100).toString() : "0" + (d.getYear() % 100)
	);

	str = str.replace(
		/MM/,
		d.getMonth() > 9 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1)
	);
	str = str.replace(/M/g, d.getMonth() + 1);

	str = str.replace(/w|W/g, Week[d.getDay()]);

	str = str.replace(
		/dd|DD/,
		d.getDate() > 9 ? d.getDate().toString() : "0" + d.getDate()
	);
	str = str.replace(/d|D/g, d.getDate());

	str = str.replace(
		/hh|HH/,
		d.getHours() > 9 ? d.getHours().toString() : "0" + d.getHours()
	);
	str = str.replace(/h|H/g, d.getHours());

	str = str.replace(
		/mm/,
		d.getMinutes() > 9 ? d.getMinutes().toString() : "0" + d.getMinutes()
	);
	str = str.replace(/m/g, d.getMinutes());

	str = str.replace(
		/ss|SS/,
		d.getSeconds() > 9 ? d.getSeconds().toString() : "0" + d.getSeconds()
	);
	str = str.replace(/s|S/g, d.getSeconds());

	return str;
}

export {
	isAscii,
	trimLeft,
	trimRight,
	trim,
	desensitizeSubstring,
	readTagAsString,
	decodeChinese,
	formartDcmDate,
	formartDcmTime,
	IsMobile,
	IsPhone,
	IsPC,
	getNSpace,
	formatDate
};
